﻿using System.Data;
using System.Data.Entity;
using System.Data.Entity.Validation;
using Example.Base.Entities;
using Example.Data.Context.Interfaces;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Collections.Generic;
using Example.Base.Interfaces.DataSources;
using System.Linq;
using System.Data.Common;

namespace Example.Data.Context
{
    public class DataContext: DbContext, IDataContext, IDatabase
    {
        public IDbSet<User> Users { get; set; }

        public IDbSet<Log> Logs { get; set; }

        public DataContext()
        {
            Initialize();
        }
        
        protected void Initialize()
        {
            Users = Set<User>();
            Logs = Set<Log>();

            Configuration.ProxyCreationEnabled = false;
        }

        public new void SaveChanges()
        {
            base.SaveChanges();
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();            
        }

        #region IDatabase implementation
        IQueryable<User> IDatabase.Users 
        {
            get { return Users; }            
        }

        IQueryable<Log> IDatabase.Logs 
        { 
            get { return Logs; }
        }
        #endregion
    }
}
